﻿using Project.Model;
using Project.BLL.MemberBll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Web.Http;
using Project.API.Models.Member;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace Project.API.Controllers.MemberContrllers
{
    public class MemberController : ApiController
    {
        MemberBll bll = new MemberBll();

        // GET api/<controller> 
        /// <summary>
        /// 查询会员信息列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="size">每页页数</param>
        /// <param name="Type">根据类型查询</param>
        /// <param name="UserName">根据会员人名称查询</param>
        /// <returns>会员信息集合</returns>
        public ResultPageList<MemberDto> GetList(int page, int size, string Type = "", string UserName = "")
        {
            ResultPageList<MemberDto> list = new ResultPageList<MemberDto>();

            var data = bll.QueryList();

            //判断条件
            if (!string.IsNullOrWhiteSpace(Type))
            {
                data = data.Where(x => x.MemberType.Mtype == Type).ToList();
            }
            else if (!string.IsNullOrWhiteSpace(UserName))
            {

                data = data.Where(x => x.user_info.username.Contains(UserName)).ToList();

            }
            else if (!string.IsNullOrWhiteSpace(Type) && !string.IsNullOrWhiteSpace(UserName))
            {

                data = data.Where(x => x.user_info.username.Contains(UserName) && x.user_info.username == UserName).ToList();
            }

            //赋值条数
            list.count = data.Count;

            //分页数据

            data = data.OrderBy(x => x.Bid).Skip((page - 1) * size).Take(size).ToList();

            //转换Dto
            list.data = data.Select(x => new MemberDto
            {
                Bid = x.Bid,
                CreateDate = x.CreateDate,
                EndDate = x.EndDate,
                Uid = x.Uid,
                Mid = x.Mid,
                UserName = x.user_info.username,
                Mtype = x.MemberType.Mtype
            }).ToList();

            return list;
        }

        // GET api/<controller>/5
        public List<MemberAllDto> Get(int id)
        {
            string connectionString = "Data Source=cs;Initial Catalog=chenshun_S3;Integrated Security=True;";
            List<MemberAllDto> list = new List<MemberAllDto>();
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    // 使用参数化查询
                    string sqlQuery = @"select * from Related r 
                                    inner join MemberType t on r.mid=t.Mid
                                    inner join MembershipRights s on s.Rid=r.Rid
                                    inner join Member m on m.mid=t.Mid where m.Uid=@Uid";
                    using (SqlCommand command = new SqlCommand(sqlQuery, connection))
                    {
                        // 添加参数
                        command.Parameters.AddWithValue("@Uid", id);

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    list.Add(new MemberAllDto
                                    {
                                        Mid = reader.GetInt32(reader.GetOrdinal("Mid")),
                                        Mtype = reader.GetString(reader.GetOrdinal("Mtype")), // 假设 Mtype 是字符串类型
                                        Rname = reader.GetString(reader.GetOrdinal("Rname")), // 假设 Rname 是字符串类型
                                        Description = reader.GetString(reader.GetOrdinal("Description")),// 假设 Description 是字符串类型
                                        CreateDate = reader.GetDateTime(reader.GetOrdinal("CreateDate")),
                                        EndDate = reader.GetDateTime(reader.GetOrdinal("EndDate")),
                                        Uid = reader.GetInt32(reader.GetOrdinal("Uid"))
                                    });
                                }
                                return list;
                            }
                            else
        {
                                return list;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            return list;
        }

        // POST api/<controller> 新增会员
        public Result Post([FromBody] MemberEdit value)
        {
            Member member = new Member()
            {
                Bid = value.Bid,
                CreateDate = value.CreateDate,
                Uid = value.Uid,
                Mid = value.Mid
            };

            Result result = new Result();
            result.code = bll.Add(member) ? 1 : 0;


            return result;
        }

        // PUT api/<controller>/5  修改会员
        public Result Put([FromBody] MemberDto value)
        {
            Result result = new Result();
            Member member = new Member() { 
                    Bid= value.Bid,
                    Mid= value.Mid,
                    CreateDate= value.CreateDate,
                    Uid= value.Uid
            };
            MemberBll bll = new MemberBll();

            result.code=bll.Update(member) ?1:0;

            return result;
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
}
}